package com.spa.infrastructure.repository.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.WithdrawAccountCondition;
import com.spa.infrastructure.repository.mapper.WithdrawAccountMapper;
import com.spa.infrastructure.repository.po.WithdrawAccountPO;
import com.spa.infrastructure.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class WithdrawAccountRepositoryService extends ServiceImpl<WithdrawAccountMapper, WithdrawAccountPO> {
    @Resource
    private WithdrawAccountMapper withdrawAccountMapper;

    public void add(WithdrawAccountPO po) {
        withdrawAccountMapper.insert(po);
    }
    public boolean update(WithdrawAccountPO po) {
       return update(updateWrapper(po));
    }

    public void upsert(WithdrawAccountPO po) {
        if (!update(po)){add(po);}
    }
    public List<WithdrawAccountPO>  getWithdrawAccountPOLast(WithdrawAccountCondition condition) {
        return list(wrapper(condition));
    }

    /**
     * 技师或门店最新的一条提现账户数据
     * @param condition
     * @return
     */
    private Wrapper<WithdrawAccountPO> wrapper(WithdrawAccountCondition condition) {
        return new LambdaUpdateWrapper<WithdrawAccountPO>()
                .eq(StringUtils.isNotNull(condition.getMainId()),WithdrawAccountPO::getMainId, condition.getMainId())
                .eq(StringUtils.isNotNull(condition.getType()), WithdrawAccountPO::getType, condition.getType())
                .orderByDesc(WithdrawAccountPO::getCreateTime);
    }

    private LambdaUpdateWrapper<WithdrawAccountPO> updateWrapper(WithdrawAccountPO po) {
        LambdaUpdateWrapper<WithdrawAccountPO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StringUtils.isNotNull(po.getType()),WithdrawAccountPO::getType, po.getType());
        wrapper.eq(StringUtils.isNotNull(po.getMainId()),WithdrawAccountPO::getMainId, po.getMainId());
        wrapper.eq(StringUtils.isNotNull(po.getBankName()),WithdrawAccountPO::getBankName, po.getBankName());
        wrapper.eq(StringUtils.isNotNull(po.getBankAccount()),WithdrawAccountPO::getBankAccount, po.getBankAccount());
        wrapper.set(StringUtils.isNotNull(po.getBankDetail()),WithdrawAccountPO::getBankDetail, po.getBankDetail());
        wrapper.set(StringUtils.isNotNull(po.getBankNumber()),WithdrawAccountPO::getBankNumber, po.getBankNumber());
        wrapper.set(StringUtils.isNotNull(po.getBankPic()),WithdrawAccountPO::getBankPic, po.getBankPic());
        wrapper.set(WithdrawAccountPO::getUpdateTime, LocalDateTime.now());
        return wrapper;
    }
}
